package com.zttek.photosShareProject.modules.web.login;

import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.utils.ImgCompressUtil;
import com.zttek.photosShareProject.common.utils.OSSClientUtil;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.login.impl.UserServiceImpl;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 13296 on 2017/8/14.
 */
@Controller
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IImageService iImageService;

    @RequestMapping(value = "user/list",method = RequestMethod.GET)
    public User listView(Model model) {
        List<User> list = null;
        try {
            list = userService.selectAllUser();
            model.addAttribute("userList",list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "user/list",method = RequestMethod.POST)
    @ResponseBody
    public User list(Model model) {
        List<User> list = null;
        Page<User> page = new Page<>(1,5);
        try {
            //list = userService.selectAllUser();
            //page = userService.selectUserPage(page);
            //list = page.getRecords();
//            page = userService.selectLike(page,"c");
            System.out.println("size:"+list.size());
            for(User u:list){
                System.out.println(u.getUsername());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list.get(0);
    }

    /**
     * 编辑用户GET
     * @param model
     * @return
     */
    @RequestMapping(value = "user/edit",method = RequestMethod.GET)
    public String editUserView(Long id,Model model){
        User user = null;
        try {
            user = userService.selectUserById(id);
            model.addAttribute("user",user);

            //获取用户头像（原图和缩略图）
            Map<String,String> imgMap = new HashMap<>();

            model.addAttribute("imgMap",imgMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "user/edit",method = RequestMethod.POST)
    public String editUser(Long id, String password, String rpassword, String username, Integer sex,
                           String hobbies, Date birthday, String address, String mobilephone, String email,
                           String job, Model model, @RequestParam("imgFile") MultipartFile imgFile,HttpServletRequest request){
        User user = null;
        try {
            user = userService.selectUserById(id);

            user.setUsername(username);
            user.setSex(sex);
            user.setHobbies(hobbies);
            user.setBirthday(birthday);
            user.setAddress(address);
            user.setMobilephone(mobilephone);
            user.setEmail(email);
            user.setJob(job);

            //头像处理
            if(!imgFile.isEmpty()){

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping(value = "user/setIsShare",method = RequestMethod.GET)
    public String setIsShareView(Model model){
        return null;
    }

    @RequestMapping(value = "user/setIsShare",method = RequestMethod.POST)
    public String setIsShare(){
        return null;
    }


    @RequestMapping(value = "user/uploadPhotots",method = RequestMethod.GET)
    public String uploadPhotosView(){
        return null;
    }


    @RequestMapping(value = "user/uploadPhotots",method = RequestMethod.POST)
    public String uploadPhotos(@RequestParam("imgFiles") MultipartFile[] imgFiles){
        /*上传多图片*/
        if(imgFiles!=null&&imgFiles.length>0){
            System.out.println("sizeC:"+imgFiles.length);
            for (int i=0;i<imgFiles.length;i++){
                if(!imgFiles[i].isEmpty()){
                    try {
                        iImageService.getImageMD5Val(imgFiles[i]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //转存图片
                    //FileUtils.copyInputStreamToFile(imgFiles[i].getInputStream(),new File("C:\\Users\\13296\\Desktop\\流程图\\upload\\"+System.currentTimeMillis()+imgFiles[i].getOriginalFilename()));
                    OSSClientUtil ossClientUtil = new OSSClientUtil();
                    String msg = ossClientUtil.uploadImg2Oss(imgFiles[i]);//上传原图片到阿里云
                    //System.out.println("msg:"+msg);
                    String path = ossClientUtil.getImgUrl(msg).split("[?]")[0];
                    System.out.println(i+"path:"+path);
                    //压缩图片
                    String url = ImgCompressUtil.getCompress(imgFiles[i]);
                    String thumbMsg = ossClientUtil.uploadImg2Oss(url);//上传压缩图片到阿里云
                    String thumbPath = ossClientUtil.getImgUrl(thumbMsg).split("[?]")[0];
                    System.out.println(i+"thumbPath:"+thumbPath);
                }
            }
        }
        return null;
    }
}
